"ui";

ui.statusBarColor("#FFFFFF"); //通知栏颜色
setTimeout(Main, 500);
var contextWrong = null;
var context_NowZT = "正在获取脚本地址……";
var contextOrangeJs;
function Main(){
    ui.layout(
        <frame background="#FFFFFF">//全局背景颜色
            <vertical paddingTop="5" margin="30 15 30 200" gravity="center">
                <img src="https://gitee.com/wupk/auto.test/raw/master/ic_launcher.png" h="50"/>//应用logo
                <text text="大白助手" textStyle="bold" textColor="#000000" textSize="16sp" margin="5" gravity="center"/>
                <progressbar indeterminate="true" style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal" margin="0 20 0 0"/>
                <text id="im" textColor="#000000" textSize="12" marginTop="5" gravity="center"/>
            </vertical>
        </frame>
    );

    threads.start(function() {
        try {
            var Url = "https://gitee.com/wupk/auto.test/raw/master/App1.1Url";
            var res_script = http.get(Url, {
                headers: {
                    'Accept-Language': 'en-us,en;q=0.5',
                    'User-Agent': 'Mozilla/5.0(Macintosh;IntelMacOSX10_7_0)AppleWebKit/535.11(KHTML,likeGecko)Chrome/17.0.963.56Safari/535.11'
                }
            });
            var OrangeJs = res_script.body.string();
            AB = OrangeJs[0] + OrangeJs[1] + OrangeJs[2] + OrangeJs[3] + OrangeJs[4] + OrangeJs[5] + OrangeJs[6] + OrangeJs[7];
            if (res_script.statusCode == 200 && AB == "https://") {
                log("脚本地址请求成功");
                context_NowZT = "正在获取脚本……";
                var res_script = http.get(OrangeJs, {
                    headers: {
                        'Accept-Language': 'en-us,en;q=0.5',
                        'User-Agent': 'Mozilla/5.0(Macintosh;IntelMacOSX10_7_0)AppleWebKit/535.11(KHTML,likeGecko)Chrome/17.0.963.56Safari/535.11'
                    }
                });
                var OrangeJs = res_script.body.string();
                AB = OrangeJs[0] + OrangeJs[1] + OrangeJs[2] + OrangeJs[3] + OrangeJs[4];
                if (res_script.statusCode == 200 && AB == '"ui";') {
                    log("请求成功：" + res_script.statusCode + res_script.statusMessage);
                    context_NowZT = "请求成功，即将载入:";
                    clearTimeout(A);
                    contextWrong = 5;
                    contextOrangeJs = OrangeJs;
                } else {
                    log("网址导向错误，请联系开发者解决");
                    clearTimeout(A);
                    contextWrong = 3;
                }
            } else {
                log("脚本地址1请求失败！" + res_script.statusCode + res_script.statusMessage);
            }
        } catch (e) {
            log("网络连接错误:" + e);
            clearTimeout(A);
            contextWrong = 1;
        }
    });

    var A = setTimeout(function() {
        contextWrong = 2;
        log("超时！线程取消:" + threads.shutDownAll());
    }, 20000);

    var B = setInterval(function() {
        try {
            ui.im.setText(context_NowZT);
        } catch (e) {
            log(e);
        }
        if (contextWrong == 1) {
            TryAgain();
            log("网络未连接!线程取消:" + threads.shutDownAll());
            clearInterval(B);
        } else if (contextWrong == 3) {
            WrongScript();
            log("网络未连接!线程取消:" + threads.shutDownAll());
            clearInterval(B);
        } else if (contextWrong == 4) {
            WrongUrl();
            log("网络未连接!线程取消:" + threads.shutDownAll());
            clearInterval(B);
        } else if (contextWrong == 2) {
            TimeOut();
            log("超时!线程取消:" + threads.shutDownAll());
            clearInterval(B);
        } else if (contextWrong == 5) {
            clearInterval(B);
            engines.execScript("test", contextOrangeJs);
            ui.finish();
        } else if (contextWrong == 404) {
            Notfound();
            log("404!线程取消:" + threads.shutDownAll());
            clearInterval(B);
        }
    }, 1);
}

function TryAgain() { //网络连接错误

    contextWrong = null;
    ui.statusBarColor("#EBEBEB"); //通知栏颜色
    ui.layout(
        <frame background="#FFFFFF">//全局背景颜色
            <vertical align="left" paddingTop="5" margin="20 5 20 0">
                <linear orientation="horizontal" align="left" margin="0" paddingTop="0">
                    <img src="https://gitee.com/wupk/auto.test/raw/master/ic_launcher.png" h="50"/>//应用logo
                </linear>
                <text text="大白助手" textStyle="bold" textColor="#000000" textSize="16sp" margin="5" gravity="center"/>
                <text text="无可用网络连接" textStyle="bold" textColor="#000000" textSize="24sp" margin="0 10 0 0"/>
                <text autoLink="all" text="很抱歉，本软件需要完全的网络访问权限来联网获取并运行代码，您必须授予本软件“联网权限”以及保持通畅的网络连接才能正常使用。" textColor="#000000" textSize="14sp" margin="0 10 0 0" lineSpacingExtra="5"/>
                <button id="c" w="*" h="40" text="点击重试" textColor="#FFFFFF" textSize="15sp" bg="#03A9F4" margin="0 40 0 0" style="?android:attr/borderlessButtonStyle" gravity="center"/>
                <button id="e" w="*" h="40" text="查看日志" textColor="#FFFFFF" textSize="15sp" bg="#4CAF50" margin="0 10 0 0" style="?android:attr/borderlessButtonStyle" gravity="center"/>
                <button id="d" w="*" h="40" text="退出软件" textColor="#FFFFFF" textSize="15sp" bg="#FF9800" margin="0 10 0 0" style="?android:attr/borderlessButtonStyle" gravity="center"/>
            </vertical>
        </frame>
    );
    ui.c.click(() => {
        Main();
    });
    ui.d.click(() => {
        ui.finish();
        exit();
    });
    ui.e.click(() => {
        app.startActivity({
            action: "android.intent.action.VIEW",
            packageName: "com.w.auojs.turbo",
            className: "com.w.auojs.turbo.LogActivity"
        });
    });
}

function WrongScript() {
    ui.statusBarColor("#EBEBEB"); //通知栏颜色
    ui.layout(
        <frame background="#FFFFFF">//全局背景颜色
            <vertical paddingTop="5" margin="20 40 20 55" gravity="center">
                <img src="https://gitee.com/wupk/auto.test/raw/master/ic_launcher.png" h="50"/>//应用logo
                <text text="大白助手" textStyle="bold" textColor="#000000" textSize="16sp" margin="5" gravity="center"/>

                <button id="cs" text="点击重试" w="200sp" textColor="#FFFFFF" textSize="15sp" bg="#81C784" marginTop="10"/>
                <button id="e" text="查看日志" w="200sp" textColor="#FFFFFF" textSize="15sp" bg="#2196F3" margin="0 10 0 0"/>
                <text text="这个错误也可能是开发者误操作或服务器端的问题" textColor="#9E9E9E" textSize="8" marginTop="10" gravity="center"/>
                <text text="重试后若依然如此请联系开发者" textColor="#9E9E9E" textSize="8" marginTop="0" gravity="center"/>
            </vertical>
        </frame>
    );
    ui.cs.click(() => {
        Main();
    });

    ui.e.click(() => {
        app.startActivity({
            action: "android.intent.action.VIEW",
            packageName: "com.w.auojs.turbo",
            className: "com.w.auojs.turbo.LogActivity"
        });
    });
}

function TimeOut() { //请求超时
    ui.statusBarColor("#EBEBEB"); //通知栏颜色
    ui.layout(
        <frame background="#FFFFFF">//全局背景颜色
            <vertical paddingTop="5" margin="20 15 20 72" gravity="center">
                <img src="https://gitee.com/wupk/auto.test/raw/master/ic_launcher.png" h="50"/>//应用logo
                <text text="大白助手" textStyle="bold" textColor="#000000" textSize="16sp" margin="5" gravity="center"/>
                <img src="@drawable/ic_signal_cellular_connected_no_internet_0_bar_black_48dp" tint="#000000" h="50" marginTop="30"/>
                <text text="网络请求超时，请检查网络后重试" textColor="#000000" textSize="10" marginTop="5" gravity="center"/>
                <button id="cs" text="点击重试" w="200sp" textColor="#FFFFFF" textSize="15sp" bg="#81C784" marginTop="10"/>
                <button id="e"text="查看日志" w="200sp" textColor="#FFFFFF" textSize="15sp" bg="#2196F3" margin="0 10 0 0"/>
            </vertical>
        </frame>
    );
    ui.cs.click(() => {
        Main();
    });
    ui.e.click(() => {
        app.startActivity({
            action: "android.intent.action.VIEW", //此处可为其他值
            packageName: "com.w.auojs.turbo",
            className: "com.w.auojs.turbo.LogActivity"
        });
    });
}

function Notfound() {
    var i = 0;
    ui.statusBarColor("#EBEBEB"); //通知栏颜色
    ui.layout(
        <frame background="#FFFFFF">//全局背景颜色
            <vertical paddingTop="5" margin="20 22 20 55" gravity="center">
                <img src="https://gitee.com/wupk/auto.test/raw/master/ic_launcher.png" h="50"/>//应用logo
                <text text="大白助手" textStyle="bold" textColor="#000000" textSize="16sp" margin="5" gravity="center"/>
                <button id="cs" text="点击重试" w="200sp" textColor="#FFFFFF" textSize="15sp" bg="#81C784" marginTop="10"/>
                <button id="e" text="查看日志" w="200sp" textColor="#FFFFFF" textSize="15sp" bg="#2196F3" margin="0 10 0 0"/>
                <text text="这个错误也可能是开发者误操作或服务器端的问题" textColor="#9E9E9E" textSize="8" marginTop="10" gravity="center"/>
                <text text="重试后若依然如此请联系开发者" textColor="#9E9E9E" textSize="8" marginTop="0" gravity="center"/>
            </vertical>
        </frame>
    );
    ui.cs.click(() => {
        Main();
    });
    ui.e.click(() => {
        app.startActivity({
            action: "android.intent.action.VIEW", //此处可为其他值
            packageName: "com.w.auojs.turbo",
            className: "com.w.auojs.turbo.LogActivity"
        });
    });
}